package com.roadmap.client.module.task;

import java.util.Vector;

import org.apache.log4j.Logger;

import com.roadmap.client.module.BaseManager;
import com.roadmap.common.BaseConst;
import com.roadmap.common.BaseEntity;
import com.roadmap.common.RdcLogger;
import com.roadmap.common.BaseEnum.ActionType;
import com.roadmap.entities.Assessment;
import com.roadmap.entities.LifeTask;
import com.roadmap.entities.Notice;
import com.roadmap.exception.ActionNotSupportedException;
import com.roadmap.exception.EntityNotFoundException;
import com.roadmap.exception.InvalidActionException;
import com.roadmap.exception.InvalidValueException;
import com.roadmap.exception.NullObjectException;
import com.roadmap.exception.ServerException;

public class AssessmentManager extends BaseManager {
	private final static Logger logger = RdcLogger
			.getLogger(AssessmentManager.class);

	public AssessmentManager(String aspirantId, LifeTask parent) {
		super(aspirantId, Assessment.class, parent);
	}

	/**
	 * only when the task which assessment belongs to is completed, assessments
	 * can be added
	 */
	public void create(BaseEntity entity) throws NullObjectException,
			InvalidActionException, InvalidValueException, ServerException,
			EntityNotFoundException, ActionNotSupportedException {
		if (!checkEntityIsValid(entity)) {
			return;
		}

		Assessment assess = (Assessment) entity;
		LifeTask task = (LifeTask) getParent();
		// only when the task's status is COMPLETED, can an assessment be
		// created
		if (task.getStatus() == BaseConst.LIFETASK_STATUS_COMPLETED) {
			getDbUtil().save(assess);
			getNoticeManager().notifyForEntry(entity, ActionType.CREATE, null);
		}
		else {
			String msg = "only when the task (" + task.getId()
					+ ") is completed, can assessments be created.";
			ActionNotSupportedException ex = new ActionNotSupportedException();
			logger.error(msg, ex);
			throw ex;
		}
	}

}
